package org.example.servlet.family;

import com.google.gson.Gson;
import org.example.dao.FamilyDao;
import org.example.entity.BaseResponse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/delete_family")
public class DeleteFamilyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        // 设置返回的数据类型
        resp.setContentType("application/json");

        String familyid = req.getParameter("id");
        // data access obj ===> dao
        int rows = FamilyDao.deleteFamily(familyid);

        BaseResponse baseResponse = new BaseResponse();

        if (rows > 0){
            baseResponse.setCode(200);
            baseResponse.setMsg("删除成功");
        }
        else {
            baseResponse.setCode(600);
            baseResponse.setMsg("删除失败");
        }

        Gson gson = new Gson();
        // 将Java对象转化为Json字符串
        String json = gson.toJson(baseResponse);

        System.out.println(json);

        PrintWriter writer = resp.getWriter();
        writer.write(json);
        // 刷新页面
        writer.flush();
    }
}
